package com.eling.elcms.basedoc.dao.hibernate;

import com.eling.elcms.basedoc.dao.IComOrgRelationDao;
import com.eling.elcms.basedoc.model.ComOrgRelation;
import com.eling.elcms.basedoc.model.CommunityData.Status;
import com.eling.elcms.core.dao.hibernate.GenericDaoHibernate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class ComOrgRelationDaoHibernate extends GenericDaoHibernate<ComOrgRelation, Long> implements IComOrgRelationDao{

	public ComOrgRelationDaoHibernate() {
		super(ComOrgRelation.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ComOrgRelation> queryCom(ComOrgRelation cond) {
		
		if(null == cond.getOrganization()){
			return new ArrayList<>();
		}
		
		String hql = "from ComOrgRelation "
				+ "where organization.pkOrganization=:pkOrg "
				+ "	and communityData.status='"+Status.Setting+"'";
		if(cond.getCommunityData() != null){
			hql += "	and communityData.address.code like('"+cond.getCommunityData().getAddress().getCode()+"%')";
		}
				
		return (List<ComOrgRelation>)getSession().createQuery(hql).setParameter("pkOrg", cond.getOrganization().getPkOrganization()).list();
	}
}
